package cn.sjhh.flow.conf;


import java.util.Properties;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.bstek.uflo.console.UfloServlet;
import com.bstek.uflo.env.EnvironmentProvider;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
@EnableTransactionManagement
@ImportResource({"classpath:uflo-console-context.xml"})
public class FlowHibernateConf {
	
	@Bean
    public ServletRegistrationBean<UfloServlet> servletRegistration() {
        return new ServletRegistrationBean<>(new UfloServlet(), "/uflo/*");
    }
	
	@Bean
	public EnvironmentProvider FlowEnvironmentProvider(TransactionManager transactionManager,
																					   LocalSessionFactoryBean localSessionFactoryBean) {
		FlowEnvironmentProvider provider=new FlowEnvironmentProvider();
		provider.setSessionFactory(localSessionFactoryBean.getObject());
		provider.setPlatformTransactionManager((PlatformTransactionManager)transactionManager);
		return provider;
	}
	
	@Bean
    public LocalSessionFactoryBean localSessionFactoryBean(HikariDataSource  dataSource) {
        LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
        localSessionFactoryBean.setDataSource(dataSource);
        localSessionFactoryBean.setPackagesToScan("com.bstek.uflo.model.**");
        Properties properties = new Properties();
        properties.setProperty("hibernate.dialect","org.hibernate.dialect.MySQL5Dialect");
        properties.setProperty("hibernate.show_sql", "true");
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        localSessionFactoryBean.setHibernateProperties(properties);        
        return localSessionFactoryBean;
    }	
}
